package com.example.videoworks.mapper;

import com.example.videoworks.entity.Work;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.security.PublicKey;
import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author Lily
 * @since 2023-05-11
 */
@Mapper
public interface WorkMapper extends BaseMapper<Work> {

    @Select("SELECT * FROM work WHERE title LIKE CONCAT('%',#{title},'%') order by startYear desc")
    public List<Work> searchTitle(String title);

    @Select("SELECT *, @row_number := @row_number + 1 AS rank\n" +
            "FROM work, ( select @row_number := 0) r\n" +
            "WHERE type LIKE CONCAT('%',#{type},'%')\n" +
            "ORDER BY rating DESC\n")
    public  List<Work> rankOneTypeWorkRating(String type);

    @Select("SELECT *, @row_number := @row_number + 1 AS rank\n" +
            "FROM work, ( select @row_number := 0) r\n" +
            "WHERE type LIKE CONCAT('%',#{type},'%')\n" +
            "ORDER BY startYear DESC\n")
    public  List<Work> rankOneTypeWorkByStartyear(String type);

    @Select("SELECT * " +
            "FROM work " +
            "WHERE type LIKE CONCAT('%',#{type},'%') " +
            "AND CONCAT(IFNULL(title,''),IFNULL(region,''),IFNULL(workIntroduction,''),IFNULL(genres,'')) LIKE CONCAT('%',#{searchParam},'%')")
    public List<Work> searchTitleOnType(String searchParam, String type);

    @Select("SELECT *\n" +
            "FROM work\n" +
            "WHERE type LIKE CONCAT('%',#{type},'%')")
    public List<Work> filterType(String type);

    @Select("SELECT * FROM worktest WHERE genres LIKE CONCAT('%',#{genre},'%')")
    public List<Work> filterGenre(String genre);

    @Select("SELECT *\n" +
            "FROM work\n" +
            "WHERE region=#{region}\n")
    public List<Work> filterRegion(String region);

    @Select("SELECT *\n" +
            "\n" +
            "FROM work\n" +
            "\n" +
            "WHERE title LIKE CONCAT('%',#{searchParam},'%')\n" +
            "\n" +
            "AND startYear BETWEEN #{lowerYear} AND #{upperYear}\n" +
            "\n" +
            "AND type LIKE CONCAT('%',#{type},'%')\n" +
            "\n" +
            "AND region LIKE CONCAT('%',#{region},'%')\n" +
            "\n" +
            "AND genres LIKE CONCAT('%',#{genre},'%')")
    public List<Work> advanceSearch(String  searchParam, String lowerYear, String upperYear, String type, String region, String genre);


    @Select("SELECT *\n" +
            "\n" +
            "FROM work\n" +
            "\n" +
            "WHERE title LIKE CONCAT('%',#{searchParam},'%')\n" +
            "\n" +
            "AND startYear BETWEEN #{lowerYear} AND #{upperYear}\n" +
            "\n" +
            "AND type LIKE CONCAT('%',#{type},'%')\n" +
            "\n" +
            "AND region LIKE CONCAT('%',#{region},'%')\n" +
            "\n" +
            "AND genres LIKE CONCAT('%',#{genre},'%')\n" +
            "ORDER BY rating DESC")
    public List<Work> SearchOrderRating(String  searchParam, String lowerYear, String upperYear, String type, String region, String genre);

    @Select("SELECT *\n" +
            "\n" +
            "FROM work\n" +
            "\n" +
            "WHERE title LIKE CONCAT('%',#{searchParam},'%')\n" +
            "\n" +
            "AND startYear BETWEEN #{lowerYear} AND #{upperYear}\n" +
            "\n" +
            "AND type LIKE CONCAT('%',#{type},'%')\n" +
            "\n" +
            "AND region LIKE CONCAT('%',#{region},'%')\n" +
            "\n" +
            "AND genres LIKE CONCAT('%',#{genre},'%')\n" +
            "ORDER BY startYear DESC")
    public List<Work> SearchOrderYear(String  searchParam, String lowerYear, String upperYear, String type, String region, String genre);


}
